import 'package:app/router/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  runApp(const MyApp());
}

class WillPopScopeTestRoute extends StatefulWidget {
  @override
  State<WillPopScopeTestRoute> createState() => _WillPopScopeTestRouteState();
  final Widget child;
  const WillPopScopeTestRoute({super.key, required this.child});
}

class _WillPopScopeTestRouteState extends State<WillPopScopeTestRoute> {
  int? firstBackTime;
  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, _) async {
        if (didPop) return;
        final now = DateTime.now().millisecondsSinceEpoch;
        if (firstBackTime == null || now - firstBackTime! > 1000) {
          Fluttertoast.showToast(
            fontSize: 12.0,
            msg: "再按一次退出程序",
            toastLength: Toast.LENGTH_SHORT,
            textColor: Colors.black87,
            gravity: ToastGravity.CENTER,
          );
          firstBackTime = now;
          return;
        }
        await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      },
      child: widget.child,
    );
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: '倒数日',
      theme: ThemeData(),
      initialRoute: UserRouter.INDEX,
      onGenerateRoute: UserRouter.onGenerateRoute,
    );
  }
}
